package com.wyl.auth.exception;


import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.wyl.common.ResponseData;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.login.LoginException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2024/7/12 10:08
 */
@RestControllerAdvice
public class GlobalException {

    /**
     * 业务异常
     * @param exception
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    public ResponseData businessException(BusinessException exception ) {
        ResponseData responseData = ResponseData.create();
        responseData.setCode(exception.getCode());
        responseData.setMsg(exception.getMsg());
        return responseData;
    }

    /**
     * 认证异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = NotLoginException.class)
    public ResponseData authenticationException(NotLoginException e) {
        ResponseData responseData = ResponseData.create();
        responseData.setCode(HttpStatus.UNAUTHORIZED.value());
        responseData.setMsg(e.getMessage());
        return responseData;
    }

    /**
     * 授权异常
     * @return
     */
    @ExceptionHandler(value = NotPermissionException.class)
    public ResponseData  authorizationException(NotPermissionException e) {
        ResponseData responseData = ResponseData.create();
        responseData.setCode(HttpStatus.FORBIDDEN.value());
        responseData.setMsg(e.getMessage());
        return responseData;
    }

    /**
     * sql异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = SQLIntegrityConstraintViolationException.class)
    public ResponseData sQLIntegrityConstraintViolationException(Exception e) {
        ResponseData responseData = ResponseData.create();
        responseData.setCode(-1);
        responseData.setMsg(e.getMessage());
        return responseData;
    }


}
